import { ESObjectsManager } from 'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main';
import { PointCollection } from './ThingManager/PointCollection';
import { AlarmCollection } from "./ThingManager/AlarmCollection"
import { FenceCollection } from "./ThingManager/FenceCollection"
import { PersonCollection } from "./ThingManager/PersonCollection"
import { getPointInfo, getFenceInfo } from '../api/api'
import { WebSocketClient } from "../api/ws"
import { isJSON, guid } from "./common/utils";

export class MyProjectManager extends ESObjectsManager {
    constructor() {
        super();

        // 点位管理器
        this._pointCollection = this.disposeVar(new PointCollection(this))

        // 围栏管理器
        this._fenceCollection = this.disposeVar(new FenceCollection(this))

        // 人员管理器和数据
        this._personCollection = this.disposeVar(new PersonCollection(this))
        this._personWebSocketClient = new WebSocketClient('ws://192.168.0.15:8887/position')

        // 报警管理器和数据
        this._alarmCollection = this.disposeVar(new AlarmCollection(this))
        this._alarmWebSocketClient = new WebSocketClient('ws://192.168.0.15:8887/alarm')

        this.getPoints()
        this.getPersonPosition()
        this.getAlarm()
        this.getFence()

        this.json = {
            // @ts-ignore
            "asset": {
                "type": "ESObjectsManager",
                "version": "0.1.0",
                "createdTime": "2022-06-17T05:54:41.744Z",
                "modifiedTime": "2024-07-10T00:44:25.580Z",
                "name": "图层管理"
            },
            "viewers": [
                {
                    "id": "61a002c6-8e46-4b3a-92c8-b5b8560d2f41",
                    "type": "ESCzmViewer",
                    "show": true,
                    "globeShow": true,
                    "ionAccessToken": "",
                    "apiKey": "",
                    "apiUrl": "",
                    "secretKey": "",
                    "speechVoiceMode": "",
                    "hoverTime": 2,
                    "debug": false,
                    "lonLatFormat": "DECIMAL_DEGREE",
                    "currentTime": 1720584000000,
                    "simulationTime": 1720540800000,
                    "timeSync": false,
                    "rain": 0,
                    "snow": 0,
                    "cloud": 0,
                    "fog": 0,
                    "depthOfField": 0,
                    "atmosphere": true,
                    "sun": true,
                    "moon": true,
                    "name": "undefined_ined"
                }
            ],
            "sceneTree": {
                "root": {
                    "children": [
                        {
                            "name": "新建场景",
                            "children": [
                                {
                                    "name": "全球影像",
                                    "sceneObj": {
                                        "id": "ae103185-08c7-4ed0-b6d4-15ad77bbbf66",
                                        "type": "ESImageryLayer",
                                        "url": "https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}",
                                        "rectangle": [
                                            -180,
                                            -90,
                                            180,
                                            90
                                        ],
                                        "maximumLevel": 18,
                                        "name": "全球影像",
                                        "allowPicking": true
                                    },
                                    "children": []
                                }
                            ]
                        }
                    ]
                }
            },
            "viewCollection": [],
            "lastView": {
                "position": [
                    105,
                    30.023157648545492,
                    18388467.771032583
                ],
                "rotation": [
                    359.99999999999994,
                    -89.89742568964756,
                    0
                ]
            }
        }


    }

    getPoints() {
        getPointInfo().then(res => {
            this._pointCollection.setData(res.data)
        })
    }
    getPersonPosition() {
        this._personWebSocketClient.ws.onmessage = (e) => {
            if (!isJSON(e.data)) return
            let data = JSON.parse(e.data).personPosition
            this._personCollection.setData(data)
        }
    }
    getAlarm() {
        this._alarmWebSocketClient.ws.onmessage = (e) => {
            if (!this._alarmCollection.switch) { return }
            if (!isJSON(e.data)) return
            let data = JSON.parse(e.data)
            data.id = guid()
            data.createTime = Date.now()
            this._alarmCollection.setData(data)
        }
    }

    getFence() {
        getFenceInfo().then(res => {
            this._fenceCollection.setData(res.data)
        })
    }
}